﻿using System;
using System.Text;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
@TestRunner.Imports


namespace @Test.Namespace
{
	
    public partial class @Test.ClassName
    {


        @{
 
            foreach (var property in Class.Properties.Where(p => !p.IsIndexer && p.CanRead && p.CanWrite && p.Type.HasCommonValues))
            { 

        
        <text>
        @Tag("INotifyPropertyChanged")
        @TestAttribute
        public void @(Test.ClassName + "_Property_" + property.Name + "_NotifyPropertyChanged_SelfAssign") ()
        {
            var objectUnderTest = CreateObject();
            var events = new List<PropertyChangedEventArgs>();
            ((INotifyPropertyChanged)objectUnderTest).PropertyChanged += (s, e) => events.Add(e);


			//Setting the value to the current value should always work and not raise any events
			var originalValue = objectUnderTest.@property.Name;
			objectUnderTest.@property.Name = originalValue;
			@Assert.IsTrue("events.Count() == 0", "\"No change events should have been raised, the property's value didn't change.\"")
        }    
        </text>
            
            
        <text>
        @Tag("INotifyPropertyChanged")
        @TestAttribute
        public void @(Test.ClassName + "_Property_" + property.Name + "_NotifyPropertyChanged") ()
        {
            var objectUnderTest = CreateObject();
            var events = new List<PropertyChangedEventArgs>();
            ((INotifyPropertyChanged)objectUnderTest).PropertyChanged += (s, e) => events.Add(e);

            //perform a self-assignment to avoid duplicating the above test
   			var originalValue = objectUnderTest.@property.Name;
			objectUnderTest.@property.Name = originalValue;

        </text>



                foreach (var testValue in property.Type.CommonValues)
                { 
            <text>
            events.Clear();
            try
            {
                @property.Type.FullName expectedValue = @testValue;
                var valueChanged = !Object.Equals(expectedValue, objectUnderTest.@property.Name);
                objectUnderTest.@property.Name = expectedValue;
                
            	if (valueChanged)
					@Assert.IsTrue("events.Any(e => e.PropertyName == \"" + property.Name + "\")", "\"No property changed event was raised for \\\"" + property.Name + "\\\".\"")
				else
					@Assert.IsTrue("events.Count() == 0", "\"No change events should have been raised, the property's value didn't change.\"")

            }
            catch (ArgumentException)
            {
                //OK
                return;
            }
            </text>
                }

                           
        <text>
        }    
        </text>
            }
    
        }
    }
}

